//
//  NewsTableViewCell.m
//  10-CustomTableViewCell
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "NewsTableViewCell.h"
#import "UIView+Frame.h"
#import "NewsObject.h"
@interface NewsTableViewCell ()
@property (nonatomic, strong) UIImageView *viewImage;
@property (nonatomic, strong) UILabel *labelOfTitle;
@property (nonatomic, strong) UILabel *labelOfdescriprion;
@property (nonatomic, strong) UILabel *labelOfCountsLabel;
@end
@implementation NewsTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
               self.viewImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        //        _viewImage.image = [UIImage imageNamed:@"1"];
        self.labelOfTitle = [[UILabel alloc]initWithFrame:CGRectZero];
        self.labelOfdescriprion = [[UILabel alloc] initWithFrame:CGRectZero];
        self.labelOfCountsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_viewImage];
        // label上不要加视图
        [self.contentView addSubview:_labelOfdescriprion];
        [self.contentView addSubview:_labelOfTitle];
        [_labelOfdescriprion addSubview:_labelOfCountsLabel];
        //        _labelOfTitle.backgroundColor = [UIColor redColor];
        //        _labelOfdescriprion.backgroundColor = [UIColor cyanColor];
        //        _labelOfCountsLabel.backgroundColor = [UIColor blueColor];
        _labelOfdescriprion.numberOfLines = 0;
    }
    return self;
}

- (void)layoutSubviews {
    // 千万不要忘记调用父类(super)
    [super layoutSubviews];
    NSLog(@"%s时, cell的frame是%@", __FUNCTION__, NSStringFromCGRect(self.frame));
    // layoutSubViews只负责给子视图布局
    // 不要使用cell的frame去进行视图的布局
    // 使用contenView的frame进行布局
    CGFloat leftMargin = 5;
    CGFloat topMargin = 5;
    CGFloat bottomMargin = 5;
    CGFloat rightMargin = 5;
    
    _viewImage.frame = CGRectMake(leftMargin, topMargin, self.contentView.dm_width / 3, self.contentView.dm_height - topMargin - bottomMargin);
    
    CGFloat labelTitleX = leftMargin + _viewImage.dm_width + rightMargin;
    
    _labelOfTitle.frame = CGRectMake(labelTitleX, topMargin, self.contentView.dm_width - labelTitleX - rightMargin, 30);
    
    _labelOfdescriprion.frame = CGRectMake(labelTitleX, 40, _labelOfTitle.dm_width, self.contentView.dm_height - 40);
    // 1.判断父视图是不是原父视图
    //    if (_labelOfCountsLabel.superview == _labelOfdescriprion) {
    CGFloat countsx = _labelOfdescriprion.dm_width * 3 / 4;
    CGFloat countWidth = _labelOfdescriprion.dm_width * 1 / 4;
    CGFloat countHeight = _labelOfdescriprion.dm_height / 3;
    CGFloat countY = _labelOfdescriprion.dm_height * 2 / 3;
    _labelOfCountsLabel.frame = CGRectMake(countsx, countY, countWidth, countHeight);
    [_labelOfdescriprion addSubview:_labelOfCountsLabel];
    CGRect newRect = [_labelOfCountsLabel.superview convertRect:_labelOfCountsLabel.frame toView:self.contentView];
    _labelOfCountsLabel.frame = newRect;
    [self.contentView addSubview:_labelOfCountsLabel];
    //    }
}
- (void)setNewsObject:(NewsObject *)newsObject {
    _newsObject = newsObject;
    _viewImage.image = [UIImage imageNamed:newsObject.newsImage];
    _labelOfTitle.text = newsObject.title;
    _labelOfdescriprion.text = newsObject.newsDescription;
    _labelOfCountsLabel.text = newsObject.newsNumberCount;
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
